package com.huan.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 作者： huan~
 *
 * @Date 2022-04-12 20:42
 * @Description
 */
public class UserDao extends BaseDao {
    private Connection conn;
    //    private Statement stmt;
    private PreparedStatement pstmt; // 预防sql注入
    private ResultSet rs;

    /**
     * 查询数据库 - news_user
     *
     * @param username
     */
    public void getUserList(String username)
    {
        String sql = "select * from news_user where username like ?";
        rs = executeSQL(sql, new Object[]{username});
        // 4.获取查询结果
        try {
            while (rs != null && rs.next()) {
                int id = rs.getInt("id");
                String name = rs.getString("username");
                String password = rs.getString("password");
                String email = rs.getString("email");
                String usertype = rs.getString("usertype");
                System.out.println("Id: " + id + ", 用户名: " + name + ", 密码: " + password +
                                           ", 邮箱: " + email + ", 用户类型: " + usertype + "\n");
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
        finally {
            closeResource();
        }
    }


    /**
     * 修改用户名 news_user
     *
     * @param username
     * @param id
     * @return
     */
    public void updateNews(String username, int id)
    {
        String sql = "update news_user set username=? where id=?";
        int i = executeUpdate(sql, new Object[]{username, id});
        if (i > 0) {
            System.out.println("id: " + id + "修改成功");
            new UserDao().getUserList(username);
        } else {
            System.out.println("id: " + id + "修改失败，请重新修改");
        }
    }

    /**
     * 添加一个用户
     *
     * @param username
     * @param password
     * @param email
     * @param usertype
     * @return
     */
    public void addUser(String username, String password, String email, int usertype)
    {
        String sql = "insert into new.news_user ( username, password, email, usertype) values (?,?,?,?)";
        int i = executeUpdate(sql, new Object[]{username, password, email, usertype});
        if (i > 0) {
            System.out.println("用户: " + username + "添加成功");
            new UserDao().getUserList(username);
        } else {
            System.out.println("用户: " + username + "添加失败，请重新添加");
        }
    }

    /**
     * 根据ID删除一条用户信息
     *
     * @param id
     * @return
     */
    public void deleteUser(int id)
    {
        String sql = "delete from news_user where id=?";
        int i = executeUpdate(sql, new Object[]{id});
        if (i > 0) {
            System.out.println("id: " + id + "删除成功");
        } else {
            System.out.println("id: " + id + "删除失败，请重新删除");
        }
    }

    public static void main(String[] args)
    {
        /* 添加数据
        new UserDao().addUser("huan", "huan", "2074785600@qq.com", 0);
*/
        /* //修改
        new UserDao().updateNews("hhh", 4);*/
        /*//查询
        new UserDao().getUserList("hhh");*/
        /*删除*/
        new UserDao().deleteUser(4);
    }
}